package com.rchat.pocmini.receiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;

import com.aso.common.encrypt.AESUtils;
import com.itsdf07.alog.ALog;
import com.rchat.pocmini.R;
import com.rchat.pocmini.activity.PushMessageReceiveActivity;
import com.rchat.pocmini.global.BundleKeyGlobal;
import com.rchat.pocmini.global.HttpNodeGlobal;

import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by ljd on 2017/3/13.
 *
 * @Modify by sudf on 2017.7.5
 * 关于：JPushInterface 对应的广播action可查看以下地址
 * https://docs.jiguang.cn/jpush/client/Android/android_api/
 */

public class JpushReceiver extends BroadcastReceiver {
    private static final String TAG = "JpushReceiver";
    private NotificationManager nm;

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onReceive(Context context, Intent intent) {
        if (null == nm) {
            nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        String action = intent.getAction();
        ALog.dTag(TAG, "action = %s", action);
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(action)) {//用户注册SDK的intent ?

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {//用户接收SDK自定义消息的intent
            buildNotification(context, intent);
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {//用户接收SDK通知栏信息的intent
            //Web发送通知时客户端不需要处理任何逻辑，也会有通知栏，是不是JPush的sdk自己处理了？
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(action)) {//用户打开自定义通知栏的intent

        } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(action)) {// 接收网络变化 连接/断开 since 1.6.3

        } else {

        }
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    private void buildNotification(Context context, Intent rIntent) {
        Notification.Builder builder = new Notification.Builder(context);
        builder.setContentTitle(context.getResources().getString(R.string.app_name));
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setDefaults(Notification.DEFAULT_SOUND);
        builder.setWhen(System.currentTimeMillis());

        int typeCode;//服务器对应的应答码
        String desc;//服务器对应code的备注
        String extraMessage;//保存服务器推送下来的消息内容
        try {
            /**
             * JPushInterface.EXTRA_MESSAGE
             * 保存服务器推送下来的消息内容。
             * 对应 API 消息内容的 message 字段。
             * 对应 Portal 推送消息界面上的"自定义消息内容”字段。
             */
            extraMessage = AESUtils.getInstance().decrypt(rIntent.getExtras().getString(JPushInterface.EXTRA_MESSAGE));
            ALog.dTag(TAG, "extraMessage = %s", extraMessage);
            JSONObject obj = new JSONObject(extraMessage);
            typeCode = obj.getInt(HttpNodeGlobal.NodeCode);
            desc = obj.getString(HttpNodeGlobal.NodeDesc);
        } catch (Exception e) {
            return;
        }
        builder.setContentText(desc);
        ALog.d("typeCode = %s,desc = %s", typeCode, desc);
        Intent intent = new Intent(context, PushMessageReceiveActivity.class);
        intent.putExtra(BundleKeyGlobal.BUNDLE_JPUSH_EXTRA_MESSAGE, extraMessage);
        intent.putExtra(BundleKeyGlobal.BUNDLE_JPUSH_TYPECODE, typeCode);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setFullScreenIntent(pendingIntent, true);
        PendingIntent pendingIntent1 = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setContentIntent(pendingIntent1);

        Notification n = builder.build();
        n.flags |= Notification.FLAG_AUTO_CANCEL;//点击后自动消失
        nm.notify(1, n);
    }
}
